package com.cy.myblog.service.impl;

import com.cy.myblog.dao.MenuDaoI;
import com.cy.myblog.model.Tmenu;
import com.cy.myblog.pageModel.DataGrid;
import com.cy.myblog.service.MenuServiceI;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/7/13.
 */
@Service("menuService")
public class MenuServiceImpl implements MenuServiceI{

    private MenuDaoI menuDao;

    @Resource(name = "menuDao")
    public void setMenuDao(MenuDaoI menuDao) {
        this.menuDao = menuDao;
    }

    @Override
    public List<Tmenu> findAll() {
        String hql = "from Tmenu order by seqID";
        List<Tmenu> tmenus = menuDao.find(hql);
        if (tmenus != null && tmenus.size() > 0) {
            return tmenus;
        }
        return null;
    }

    @Override
    public void save(Tmenu menu) {
       menuDao.saveOrUpdate(menu);
    }

    @Override
    public Tmenu getById(int id) {
       return menuDao.getById(Tmenu.class,id);
    }

    @Override
    public List<Tmenu> queryByPid(int pid) {
        String hql = "from Tmenu where pid =:pid order by seqID";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pid", pid);
        return menuDao.find(hql,params);
    }

    @Override
    public Map queryMenuTree() {
        List<Tmenu> menuList =findAll();
        Tmenu menu = getById(0);
        Map root = new HashMap();

        root.put("id",menu.getId());
        root.put("name",menu.getName());
        root.put("parameter",menu.getParameter());
        root.put("icon",menu.getIcon());
        root.put("seqID",menu.getSeqID());
        root.put("memo",menu.getMemo());
        root.put("operat",menu.getOperat());
//        root.put("url",menu.getUrl());
        root.put("open",true);
        root.put("pid",menu.getPid());
        root.put("children",getChildrenNodes(menuList,menu.getId()));

        return root;
    }

    @Override
    public DataGrid queryGrid() {
        DataGrid data = new DataGrid();

        long total = menuDao.count("select count(*) from Tmenu");
        Map map = queryMenuTree();
        List list = new ArrayList();

        list.add(map);
        data.setTotal(total);
        data.setRows(list);

        return data;
    }

    @Override
    public List<Map> queryEazyUiTree() {
        List<Tmenu> menuList =findAll();
        Tmenu menu = getById(0);

        List<Map> list = new ArrayList<Map>();
        Map root = new HashMap();

        root.put("id",menu.getId());
        root.put("text",menu.getName());
        root.put("icon",menu.getIcon());
        root.put("seqID",menu.getSeqID());
        root.put("operat",menu.getOperat());
//        root.put("url",menu.getUrl());
        root.put("open",true);
        root.put("pid",menu.getPid());
        root.put("children",getEasyUiChildrenNodes(menuList, menu.getId()));
        list.add(root);

        return list;
    }

    @Override
    public void delete(int id) {
        menuDao.delete(getById(id));
    }

    public List<Map> getChildrenNodes(List<Tmenu> menuList,int pid){
        List<Map> dataList = new ArrayList<Map>();
        for (Tmenu menu:menuList)
        {
            if(menu.getPid() == pid)
            {
                Map data = new HashMap();

                data.put("id",menu.getId());
                data.put("name",menu.getName());
                data.put("parameter",menu.getParameter());
//                data.put("url",menu.getUrl());

                if(haveChildren(menu.getId())){
                    data.put("open",true);
                }

                data.put("operat",menu.getOperat());
                data.put("icon",menu.getIcon());
                data.put("seqID",menu.getSeqID());
                data.put("memo",menu.getMemo());
                data.put("pid",menu.getPid());
                data.put("children",getChildrenNodes(menuList,menu.getId()));

                dataList.add(data);
            }
        }
        return dataList;
    }

    public List<Map> getEasyUiChildrenNodes(List<Tmenu> menuList,int pid){
        List<Map> dataList = new ArrayList<Map>();
        for (Tmenu menu:menuList)
        {
            if(menu.getPid() == pid)
            {
                Map data = new HashMap();

                data.put("id",menu.getId());
                data.put("text",menu.getName());

                if(haveChildren(menu.getId())){
                    data.put("open",true);
                }

                data.put("icon",menu.getIcon());
                data.put("seqID",menu.getSeqID());
                data.put("pid",menu.getPid());
                data.put("children",getEasyUiChildrenNodes(menuList, menu.getId()));

                dataList.add(data);
            }
        }
        return dataList;
    }

    //判断是否有下级
    public boolean haveChildren(int pid)
    {
        if(queryByPid(pid)!=null)
            return true;

        return false;
    }
}
